package com.neolix.watch.domain;

import com.neolix.watch.util.DateTimeUtil;
import com.neolix.watch.util.StringUtil;

import org.joda.time.DateTime;

import java.util.ArrayList;
import java.util.List;

import static com.neolix.watch.util.StringUtil.isBlank;

public class BaiDuWeatherResult {

    private int error;
    private String status;
    private String date;

    private List<BaiDuWeatherInfo> results;

    public boolean isCorrectResults() {
        return error == 0;
    }

    public String getDateTime() {
        return date;
    }

    public String getCurrentCity() {
        if (results != null)
            return results.get(0).getCurrentCity();

        return "";
    }

    public String getCurrentTemperature() {
        if (results != null) {
            String currentDate = results.get(0).getWeatherData().get(0).getDate();
            return currentDate.substring(currentDate.indexOf("实时") + 3, currentDate.length() - 1);
        }
        return "";
    }

    public int getCurrentTemperatureValue() {
        int temperature = 0;
        String currentTemperature = getCurrentTemperature();
        if (isBlank(currentTemperature))
            temperature = 0;
        else {
            try {
                temperature = Integer.parseInt(currentTemperature.substring(0, currentTemperature.length() - 1));
            } catch (Exception ignored) {
                temperature = 0;
            }
        }

        return temperature;
    }

    public String getCurrentWeek() {
        if (results != null) {
            String currentDate = results.get(0).getWeatherData().get(0).getDate();
            return currentDate.substring(0, 2);
        }
        return "";
    }

    public BaiDuWeatherData getCurrentWeatherInfo() {
        if (results != null) {
            BaiDuWeatherData baiDuWeatherData = results.get(0).getWeatherData().get(0);
            baiDuWeatherData.setDateTime(DateTimeUtil.formatDataTimeOfyyyyMMDD(date));
            return baiDuWeatherData;
        }

        return null;
    }

    public List<BaiDuWeatherData> getFutureWeatherList() {
        List<BaiDuWeatherData> result = new ArrayList<BaiDuWeatherData>();
        if (results != null) {
            List<BaiDuWeatherData> weatherDataLis = results.get(0).getWeatherData();
            for (int i = 0; i < weatherDataLis.size(); i++) {
                if (i != 0) {
                    BaiDuWeatherData baiDuWeatherData = weatherDataLis.get(i);
                    baiDuWeatherData.setDateTime(DateTimeUtil.formatDataTimeOfyyyyMMDD(date).plusDays(i));
                    result.add(baiDuWeatherData);
                }
            }
        }

        return result;
    }

    public static class BaiDuWeatherInfo {

        private String currentCity;
        private List<BaiDuWeatherData> weather_data;

        public String getCurrentCity() {
            return currentCity;
        }

        public List<BaiDuWeatherData> getWeatherData() {
            return weather_data;
        }

    }

    public static class BaiDuWeatherData {
        private String date;
        private String dayPictureUrl;
        private String nightPictureUrl;
        private String weather;
        private String wind;
        private DateTime dateTime;
        private String temperature;

        public DateTime getDateTime() {
            return dateTime;
        }

        public void setDateTime(DateTime dateTime) {
            this.dateTime = dateTime;
        }

        public String getDate() {
            return date;
        }

        public String getDayPictureUrl() {
            return dayPictureUrl;
        }

        public String getNightPictureUrl() {
            return nightPictureUrl;
        }

        public String getWeather() {
            return weather;
        }

        public String getWind() {
            return wind;
        }

        public String getTemperature() {
            return temperature;
        }
    }
}
